﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nate_sRoguelikeGameXNA.GameObjects.Architecture;

namespace Nate_sRoguelikeGameXNA
{
    class Door
    {

        public Room Room { get; private set; }
        public MapBlock MapBlock { get; private set; }
        /// <summary>
        /// The spot right outside the door
        /// </summary>
        public Point Outside { get; set; }
        public Corridor Corridor { get; set; }

        public Door(Room room, MapBlock mapBlock)
        {
            Room = room;
            MapBlock = mapBlock;
            ComputeOutside();
        }

        public void AddToMap()
        {
            MapBlock.Architecture = GameArchitecture.WoodenDoor;
        }

        void ComputeOutside()
        {
            var p = new Point(MapBlock.Location.X, MapBlock.Location.Y);
            p = Game.Roll(1, 2) == 1 ? ComputeX(p) : ComputeY(p);

            Outside = p;
        }

        public Point ComputeX(Point p)
        {
            if (p.X == Room.OuterPerimeter.Left)
                p.X--;
            else if (p.X == Room.OuterPerimeter.Right)
                p.X++;
            return p;
        }

        public Point ComputeY(Point p)
        {
            if (p.Y == Room.OuterPerimeter.Top)
                p.Y--;
            else if (p.Y == Room.OuterPerimeter.Bottom)
                p.Y++;
            return p;
        }

        public Corridor CreateCorridorTo(Door destDoor)
        {
            Corridor = new Corridor(GameArchitecture.StonePath, this, destDoor);
            //destDoor.Corridor = Corridor;

            return Corridor;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Corridor != null)
                Corridor.Draw(spriteBatch);
            var message = new Message
                              {
                                  Color = Color.Orange,
                                  Text = "%",
                                  Location = Game.GetScreenCoordsWithOffset(MapBlock.Location)
                              };
            message.Draw(spriteBatch);

        }
    }
}
